/*
    This program downloading and printing quotes from Forismatic.com
    Copyright (C) <2012>  <Tielhalter>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    If you want help me with code, please write to <madwo@ovi.com>.
*/

#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QBitmap>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle(QString("Forismatic"));
    QPixmap image(":/images/agplv3-88x31.png");
    ui->label->setPixmap(image);
    ui->label->setMask(image.mask());
    start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::readyReadReply()
{
    answer = QString::fromUtf8(reply->readAll());
    ui->textEdit->setText(answer);
    reply->abort();
}

void Widget::getReplyFinished()
{
    reply->deleteLater();
}

void Widget::start()
{
    apiUrl = "http://www.forismatic.com/api/1.0/";
    requestString = "method=getQuote&format=text";
    QNetworkRequest request(apiUrl);
    reply = manager.post(request, requestString);
    connect(reply, SIGNAL(finished()),this, SLOT(getReplyFinished()));
    connect(reply, SIGNAL(readyRead()), this, SLOT(readyReadReply()));
}

void Widget::on_pushButton_2_clicked()
{
    start();
    ui->pushButton_2->hide();
    connect(reply, SIGNAL(finished()), ui->pushButton_2, SLOT(show()));
}

void Widget::on_pushButton_clicked()
{
    exit(0);
}
